//
//  LLEmoticonkeyboard.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/7/4.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

class LLEmoticonkeyboard: UIView {

  override  init(frame: CGRect) {
        super.init(frame: frame)
    
    setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    func setupUI(){
        
        addSubview(emoticonToolbar)
        addSubview(pageView)
        addSubview(pageControl)
    
        
        //约束
        emoticonToolbar.snp_makeConstraints { (make) -> Void in
            
            make.leading.bottom.trailing.equalTo(self)

            make.height.equalTo(37)
        }
        
        pageView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            
            make.bottom.equalTo(emoticonToolbar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(pageView)
            
            make.bottom.equalTo(pageView)
        }
        
        //实现闭包
        emoticonToolbar.toolbarClosure = {[weak self](type: LLEmoticonToolbarType) in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            //让配图滚动到指定位置
            self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            
            self?.pageControl.numberOfPages = LLEmojiTool.sharedTool.allemoticons[indexPath.section].count
            
            self?.pageControl.currentPage = 0
            
        }
        
        
        
    }
    
    ///懒加载
    //toolbar
 
    private lazy var emoticonToolbar: LLEmoticonToolbar = LLEmoticonToolbar(frame: CGRectZero)
    
    //表情键盘
    private lazy var pageView: LLEmoticonPageView = {
        
        let view = LLEmoticonPageView()
        
        view.showsHorizontalScrollIndicator = false
        
        view.showsVerticalScrollIndicator = false
        
        view.bounces = false
        
        view.pagingEnabled = true
        
        view.delegate = self
        
        return view
    }()
    
    //pageControl
    private lazy var pageControl:  UIPageControl = {
        
       let view = UIPageControl()
        
        view.currentPage = 0
        
        view.numberOfPages = LLEmojiTool.sharedTool.allemoticons[0].count
        
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return view
        
    }()
}

extension LLEmoticonkeyboard: UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let centerX = SCREENW / 2 + scrollView.contentOffset.x
        
        //把cell排序
        let cellArray = pageView.visibleCells().sort { (cell1, cell2) -> Bool in
            
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        if cellArray.count == 2{
            
            let firstCell = cellArray.first!
            
            let lastCell = cellArray.last!
            
            var indexPth: NSIndexPath
            
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
                
                indexPth = pageView.indexPathForCell(firstCell)!
                
            }else{
                
                indexPth = pageView.indexPathForCell(lastCell)!
                
            }
            
            emoticonToolbar.setCurrontButtons(indexPth.section)
            
            self.pageControl.numberOfPages = LLEmojiTool.sharedTool.allemoticons[indexPth.section].count
            
            self.pageControl.currentPage = indexPth.item
        }
        
        
    }
}
